package com.xuxin.baby.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ArrayUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.DoctorTagAdapter;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.DoctorsBean;
import com.xuxin.baby.contract.DoctorContract;
import com.xuxin.baby.glide.GlideApp;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.presenter.DoctorPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020/11/23 0023
 * 专家详情
 */
public class DoctorActivity extends BaseActivity implements DoctorContract.View {
    private final DoctorContract.Presenter presenter = new DoctorPresenter(this);
    RequestOptions options = new RequestOptions().transform(new CenterCrop(), new RoundedCorners(10));
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;

    @BindView(R.id.doctor_data)
    LinearLayout doctor_data;
    @BindView(R.id.doctor_image)
    ImageView doctor_image;
    @BindView(R.id.doctor_name)
    TextView doctor_name;
    @BindView(R.id.doctor_jobs)
    TextView doctor_jobs;
    @BindView(R.id.doctor_hospital)
    TextView doctor_hospital;
    @BindView(R.id.doctor_introduce)
    TextView doctor_introduce;
    @BindView(R.id.doctor_experience)
    TextView doctor_experience;
    @BindView(R.id.doctor_recycle_tag)
    RecyclerView doctor_recycle_tag;

    private DoctorTagAdapter myAdapter;
    private String tenantId;//租户id
    private String token;//授权token
    private String doctorId;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_doctor);
    }

    @Override
    public void initView() {
        title_name.setText("专家详情");
        myAdapter = new DoctorTagAdapter();
        RecycleUtils.chips(doctor_recycle_tag);
        doctor_recycle_tag.setAdapter(myAdapter);
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        doctorId = getIntent().getStringExtra("doctorId");
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.title_left})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getDoctor(tenantId, token, doctorId);
            }
            return false;
        }
    });


    @Override
    public void onSuccess(DoctorsBean doctorsBean) {
        doctor_name.setText(doctorsBean.getResult().getName());
        doctor_jobs.setText(doctorsBean.getResult().getPosition());
        String hospital = doctorsBean.getResult().getHospital() + " " + doctorsBean.getResult().getDepartment();
        doctor_hospital.setText(hospital);
        doctor_introduce.setText(doctorsBean.getResult().getField());
        doctor_experience.setText(dispose(doctorsBean.getResult().getIntroduction()));
        if (!StringUtils.isEmpty(doctorsBean.getResult().getTag())) {
            String[] strings = RegexUtils.getSplits(doctorsBean.getResult().getTag(), ",");
            List<String> tags = ArrayUtils.asArrayList(strings);
            myAdapter.setList(tags);
            doctor_recycle_tag.setVisibility(View.VISIBLE);
        }
        /**
         * 图片加载
         */
        String doctorImage = Retrofit.image_url + doctorsBean.getResult().getImage();
        LogUtils.e("专家详情_image = " + doctorImage);
        GlideApp.with(context).load(doctorImage).placeholder(R.mipmap.img_placeholder376).apply(options).into(doctor_image);
        doctor_data.setVisibility(View.VISIBLE);
    }

    /**
     * 根据。分段
     *
     * @param msg
     * @return
     */
    public String dispose(String msg) {
        String replace = "。\n\u3000\u3000";
        return "\u3000\u3000" + (msg.trim()).replaceAll("。", replace);
    }
}